#!/usr/bin/ruby
#-*- coding: UTF-8 -*-
require 'rexml/document'
include REXML
Encoding.default_external = Encoding::UTF_8
Encoding.default_internal = Encoding::UTF_8

data_global_subpath = "/src/gamecore/DataGlobal.lua"
sdk_lua_util_subpath = "/src/gamecore/platform/ios/SdkLuaUtil.lua"
platname_subpath = "/config/templet/platname.xml"
platstartversion_subpath = "/config/templet/platstartversion.xml"

svn_path = ARGV[0]
product_6_id = ARGV[1]
product_18_id = ARGV[2]
product_30_id = ARGV[3]
product_98_id = ARGV[4]
product_128_id = ARGV[5]
product_198_id = ARGV[6]
product_328_id = ARGV[7]
product_648_id = ARGV[8]
game_start_version = ARGV[9]
work_platform = ARGV[10]
chinese_name = ARGV[11]

#修改为UTF-8编码
chinese_name = (chinese_name.is_a? String) ? chinese_name.dup.force_encoding('UTF-8') : chinese_name.dup

puts "DataGlobal"
data_global_file = File.new("#{svn_path}#{data_global_subpath}", "r+")
if data_global_file
	contents = data_global_file.read()
	if ! contents.include? "DataGlobal.isIos#{work_platform}Platform"
		puts "start to write DataGlobal"
		add_contents = "
DataGlobal.isIos#{work_platform}Platform = function()
    local plat = gamePlatForm
    return plat==\"#{work_platform}\"
end"
		data_global_file.syswrite(add_contents)
		data_global_file.close
		puts "end write DataGlobal"
	else
		puts "#{work_platform} is exist"
	end
else
	puts "Unable to open DataGlobal file"
end

if !product_6_id.empty?
	puts "\n"
	puts "SdkLuaUtil"
	sdk_lua_file = File.new("#{svn_path}#{sdk_lua_util_subpath}", "r+")
	if sdk_lua_file
		contents = sdk_lua_file.read()
		if ! contents.include? "DataGlobal.isIos#{work_platform}Platform"
			puts "start to write SdkLuaUtil"
			add_contents = "
if DataGlobal.isIos#{work_platform}Platform() then 
	SDK_KEYS = {
    	[600] = \"#{product_6_id}\",
    	[1800] = \"#{product_18_id}\",
    	[3000] = \"#{product_30_id}\",
    	[9800] = \"#{product_98_id}\",
    	[12800] = \"#{product_128_id}\",
    	[19800] = \"#{product_198_id}\",
    	[32800] = \"#{product_328_id}\",
    	[64800] = \"#{product_648_id}\",
	} 
end"
			sdk_lua_file.syswrite(add_contents)
			sdk_lua_file.close
			puts "end write SdkLuaUtil"
		else
			puts "#{work_platform} is exist"
		end
	else
		puts "Unable to open SdkLuaUtil file"
	end
end

puts "\n"
puts "platname"
platname_file = File.new("#{svn_path}#{platname_subpath}", "r")
if platname_file
	contents = platname_file.read()
	if ! contents.include? "\"#{work_platform}\""
		puts "start to write platname"
		contents["</root>"] = "<file name=\"#{work_platform}\" ch=\"#{chinese_name}\"/>
</root>"
		write_file = File.new("#{svn_path}#{platname_subpath}", "w+")
		if write_file
   		 	write_file.syswrite(contents)
		end
		puts "end write platname"
	else
		puts "#{work_platform} is exist"
	end
else
	puts "Unable to open platname file"
end

puts "\n"
puts "platstartversion"
platstartversion_file = File.new("#{svn_path}#{platstartversion_subpath}", "r")
if platstartversion_file
	contents = platstartversion_file.read()
	if ! contents.include? "\"#{work_platform}\""
		puts "start to write platstartversion"
		contents["</root>"] = "<file name=\"#{work_platform}\" version=\"#{game_start_version}\" mainver=\"1.0.0\" agent=\"\"/>
</root>"
		write_file = File.new("#{svn_path}#{platstartversion_subpath}", "w+")
		if write_file
   		 	write_file.syswrite(contents)
		end
		puts "end write platstartversion"
	else
		puts "#{work_platform} is exist"
	end
else
	puts "Unable to open platstartversion file"
end
